package com.xnyzc.lhy.mis.controller.cmsapi.vehicle;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.cms.vehicle.AssignVehicleParam;
import com.xnyzc.lhy.mis.service.cms.vehicle.ICanAssignVehicleService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Description 可分配车辆
 * @Author zhuyubin
 * @Date 2019-08-26 15:57
 **/
@RestController
@RequestMapping(value = "cmsapi/canAssignVehicle")
public class CanAssignVehicleController {

    @Autowired
    private ICanAssignVehicleService iCanAssignVehicleService;

    /**
     * 获取司机信息并关联车辆
     * @param current 当前页
     * @return
     */
    @GetMapping(value = "selectDriverPage")
    public Rv selectDriverPage(
            @ApiParam(name = "current", value = "当前页", required = true) Long current,
            @ApiParam(name = "name", value = "司机名") String name,
            @ApiParam(name = "phone", value = "司机手机号") String phone,
            @ApiParam(name = "vehNo", value = "车牌号") String vehNo
    ){
        Long sysUserId = TokenUtil.getTokenUserId();
        // TODO 暂时不做校验
        List<String> roleDataScope = TokenUtil.getRoleDataScope();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        if (CheckUtil.objIsEmpty(current)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(iCanAssignVehicleService.selectDriverPage(sysUserId, roleDataScope, tag, current, name, phone, vehNo));
    }

    /**
     * 可分配车辆列表
     * @param current 当前页
     * @return
     */
    @GetMapping(value = "selectOaSysVehiclePage")
    public Rv selectOaSysVehiclePage(
            @ApiParam(name = "current", value = "当前页") @RequestParam(value = "current") Long current,
            @ApiParam(name = "vehNo", value = "当前页") String vehNo,
            @ApiParam(name = "officeId", value = "当前页") Long officeId
    ){
        Long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        if (CheckUtil.objIsEmpty(current)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(iCanAssignVehicleService.selectOaSysVehiclePage(sysUserId, tag, current, vehNo, officeId));
    }

    /**
     * 分配车辆
     * @param assignVehicleParam
     * @return
     */
    @PostMapping(value = "assignVehicle")
    public Rv assignVehicle(@ApiParam(name = "assignVehicleParam", value = "分配车辆入参") @RequestBody AssignVehicleParam assignVehicleParam){
        Long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        Long driverId = assignVehicleParam.getDriverId();
        Long vehicleId = assignVehicleParam.getVehicleId();
        if (CheckUtil.objIsEmpty(driverId, vehicleId)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(EErrorCode.success, iCanAssignVehicleService.assignVehicle(sysUserId, driverId, vehicleId, tag), new HashMap<>(16));
    }

    /**
     * 取消分配车辆
     * @return
     */
    @PostMapping(value = "cancelAssignVehicle")
    public Rv cancelAssignVehicle(@ApiParam(name = "assignVehicleParam", value = "取消分配车辆入参") @RequestBody AssignVehicleParam assignVehicleParam){
        Long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        Long vehicleId = assignVehicleParam.getVehicleId();
        Long driverId = assignVehicleParam.getDriverId();
        if(CheckUtil.objIsEmpty(driverId)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(EErrorCode.success, iCanAssignVehicleService.cancelAssignVehicle(sysUserId, tag, driverId, vehicleId), new HashMap<>(16));
    }
}
